using System;
using System.Data.Common;

namespace Avanzis.QueryObjects
{
    public class SessionFactory
    {
        [NonSerialized]
        private readonly ICurrentSessionContext currentSessionContext;

        #region constructor

        public SessionFactory()
		{
            currentSessionContext = BuildCurrentSessionContext();            
        }

        #endregion

        private ICurrentSessionContext BuildCurrentSessionContext()
        {
            return new ManagedWebSessionContext(this);
        }

        public Session GetCurrentSession()
        {
            if (currentSessionContext == null)
            {
                throw new QueryObjectsException("No CurrentSessionContext configured");
            }
            return currentSessionContext.CurrentSession();
        }

        public Session OpenSession()
        {
            return OpenSession(null);
        }        

        public Session OpenSession(DbConnection connection)
        {
            return new Session(connection);
            // return new Session(connection, this, (connection == null));            
        }       

          
    }
}
